package com.multipleimageselect.adapters;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.RecyclerView;
import com.asman.base.lib.glide.GlideApp;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jph.takephoto.R;
import com.multipleimageselect.activities.ImageSelectActivity;
import com.multipleimageselect.helpers.Constants;
import com.multipleimageselect.models.Image;

import java.io.File;
import java.util.List;

/**
 * Created by Darshan on 4/18/2015.
 */

public class CustomImageSelectAdapter extends RecyclerView.Adapter<CustomImageSelectAdapter.MyViewHolder> {

    private List<Image> data;

    public CustomImageSelectAdapter(List<Image> data) {
//        super(R.layout.grid_view_item_album_select, albums);
        this.data = data;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_item_image_select, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.setData(data.get(position));
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView image_view_image_select;
        AppCompatCheckBox checkBox;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            image_view_image_select = itemView.findViewById(R.id.image_view_image_select);
            checkBox = itemView.findViewById(R.id.checkBox);
        }

        public void setData(Image item) {
            itemView.setOnClickListener(v -> {
                ImageSelectActivity activity= (ImageSelectActivity) itemView.getContext();
                activity.toggleSelection(item);
                activity.toolbar.setTitle(activity.countSelected + "/" + Constants.limit);
            });
            checkBox.setChecked(item.isSelected);
            GlideApp.with(itemView.getContext())
                    .load(new File(item.path))
                    .apply(new RequestOptions().centerCrop().placeholder(new ColorDrawable(Color.parseColor("#f7f7f7"))).diskCacheStrategy(DiskCacheStrategy.NONE))
                    .into(image_view_image_select);
        }
    }
}
